/**
 * Independent time operation tool to facilitate subsequent switch to dayjs
 */
import dayjs from 'dayjs';

const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';
const DATE_FORMAT = 'YYYY-MM-DD';
const DATE_FORMAT_TIME = 'MM-DD HH:mm';

export function formatToDateTime(date: any | undefined = undefined, format = DATE_TIME_FORMAT): string {
  return dayjs(date).format(format);
}

export function formatToDate(date: dayjs.Dayjs | undefined = undefined, format = DATE_FORMAT): string {
  return dayjs(date).format(format);
}

export function formatToTime(date: dayjs.Dayjs | undefined = undefined, format = DATE_FORMAT_TIME): string {
  return dayjs(date).format(format);
}


//两个时间相差天数
export function diffDays(date1, date2) {
  const timeDiff = Math.abs(date2.getTime() - date1.getTime());
  const diffDays = Math.floor(timeDiff / (1000 * 3600 * 24));
  return diffDays;
}

export const dateUtil = dayjs;
